// pages/pay/pay.js
const app = getApp();
Page({
  data: {
    item: null,
    selectedCoupon: null, // 当前选择优惠券
    payType: "1", // 1.微信支付， 2.余额支付
    MatchType: "1",
    balanceAmout: 0.00,
    isShowCoupon: 1 // 是否显示优惠券选项
  },
  onLoad: function(options) {
    if (typeof options.isShowCoupon !== undefined && options.isShowCoupon == 0){
      this.setData({
        isShowCoupon: 0
      })
    }
    const that = this
    this.setData({
      MatchType: options.MatchType
    })
    if (options.item) {
      let item = JSON.parse(options.item)
      this.setData({
        item
      })
    }
    app.middleware.requestAjax({
      serAddr: app.serAddrs.getBalanceDetail,
      data: {},
      cancelToast: true,
      success: function (res) {
        that.setData({
          balanceAmout: res.data.balanceAmout
        })
      },
      fail: function (res) {
        app.middleware.showToast(res && res.msg || "请求失败")
      }
    })
  },
  signUp(e) {
    const that = this;
    var challengeId = e.target.dataset.id || 0;
    let totalPrice = e.target.dataset.price || 0;
    // if (totalPrice == 0 || !challengeId) return false;
    if (!challengeId) return false;
    // 判断用户登录
    var myInfo = app.isNavigateToLogin();
    if (!myInfo) return;
    myInfo = app.isNavigateToBind();
    if (!myInfo) return;
    var payParam = {};
    payParam.tV2MatchScheduleId = challengeId;
    payParam.wxMold = 1
    if (this.data.selectedCoupon){
      payParam.couponId = this.data.selectedCoupon.id
    }
    payParam.payType = this.data.payType
    payParam.MatchType = this.data.MatchType
    if (totalPrice == 0) {
      payParam.payType = 2
      this.setData({
        payType: 2
      })
    }
    app.middleware.requestAjax({
      serAddr: app.serAddrs.challengeByWXPayWithCoupon,
      data: payParam,
      cancelToast: true,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      success: function(ores) {
        console.log(ores)
        console.log('success')
        if (that.data.payType == 2) {
          wx.redirectTo({
            url: '/pages/pay/pay-success/pay-success',
          })
        }else if(that.data.payType == 1){
          that.payment(ores.data.wxPreOrder, challengeId)
        }
      },
      fail: function(res) {
        app.middleware.showToast(res && res.msg || "请求失败")
      }
    })
  },
  toCouponsList(){
    wx.navigateTo({
      url: '/pages/youhuiquan/youhuiquan/coupons',
    })
  },
  changePayType(e){
    this.setData({
      payType: e.detail.value
    })
  },
  payment(data, challengeId) {
    const that = this;
    wx.requestPayment({
      timeStamp: data.timeStamp,
      nonceStr: data.nonceStr,
      package: data.prepayId,
      signType: data.signType,
      paySign: data.paySign,
      success(res) {
        wx.navigateTo({
          url: '/pages/pay/pay-success/pay-success',
        })
      },
      fail(res) {
        app.middleware.showToast("支付失败");
      },
      complete: function(res) {
      }
    })
  }
})